package com.icss.servlets;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;

import com.icss.beans.AboutUsVO;
import com.icss.beans.User;
import com.icss.daos.AboutUsDao;

public class AboutUsServlet extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public AboutUsServlet() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doPost(request,response);
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		String action = request.getParameter("action");
		AboutUsDao aud = new AboutUsDao();
		
		if("add".equals(action)){
			String title = request.getParameter("aboutTitle");
			String body = request.getParameter("aboutBody");
			
			aud.AddAboutUs(title, body);
			request.getRequestDispatcher("AboutUsServlet?action=show").forward(request, response);
		}else if("show".equals(action)){
			ArrayList<AboutUsVO> list = aud.GetAboutUs();
			
			ArrayList<AboutUsVO> list2 = new ArrayList<AboutUsVO>();
			for(int i=0;i<list.size();i++){
				AboutUsVO a = new AboutUsVO();
				a =list.get(i);
				String p = a.getIsPublish().trim();
				a.setIsPublish(p.trim());
				list2.add(a);
			}
			request.setAttribute("list", list2);
			request.getRequestDispatcher("/page/aboutus/showAboutUs.jsp").forward(request, response);
		}else if("display".equals(action)){
			int id = Integer.parseInt(request.getParameter("aboutUSID")) ;
			
			AboutUsVO aboutus = aud.getOneAboutUs(id);
			
			request.setAttribute("aboutus", aboutus);
			request.getRequestDispatcher("/page/aboutus/displayAboutUs.jsp").forward(request, response);
		}else if("toUpdate".equals(action)){
			int id = Integer.parseInt(request.getParameter("aboutUSID")) ;
			
			AboutUsVO aboutus = aud.getOneAboutUs(id);
			
			request.setAttribute("aboutus", aboutus);
			request.getRequestDispatcher("/page/aboutus/updateAboutUs.jsp").forward(request, response);
		}else if("update".equals(action)){
			int id = Integer.parseInt(request.getParameter("aboutUSID")) ;
			String aboutTitle=request.getParameter("aboutTitle");
			String body = request.getParameter("aboutBody");
			AboutUsVO aboutus = new AboutUsVO();
			aboutus.setAboutBody(body);
			aboutus.setAboutTitle(aboutTitle);
			aboutus.setAboutUSID(id);
			if(aud.UpdateAboutUS(aboutus)){
				request.getRequestDispatcher("AboutUsServlet?action=display&aboutUSID="+id).forward(request, response);
			}
		}else if ("delete".equals(action)){
			int id = Integer.parseInt(request.getParameter("aboutUSID")) ;
			if(aud.DeleteAboutUS(id)){
				request.getRequestDispatcher("AboutUsServlet?action=show").forward(request, response);
			}
		}else if("deleteAll".equals(action)){
			String auidlist = request.getParameter("auidList");

			if (aud.deleteAllSelect(auidlist)) {
				request.getRequestDispatcher("AboutUsServlet?action=show").forward(request, response);
			}
		}else if("publish".equals(action)){
			int id = Integer.parseInt(request.getParameter("aboutUSID")) ;
			if(aud.publish(id)){
				request.getRequestDispatcher("AboutUsServlet?action=show").forward(request, response);
			}
		}else if("Unpublish".equals(action)){
			int id = Integer.parseInt(request.getParameter("aboutUSID")) ;
			if(aud.Unpublish(id)){
				request.getRequestDispatcher("AboutUsServlet?action=show").forward(request, response);
			}
		}else if("check".equals(action)){
			String result = aud.check();
			PrintWriter out = response.getWriter();			
			out.print(result);
		}else if("aboutUS".equals(action)){
			String result = aud.getText();
//			PrintWriter out = response.getWriter();			
//			out.print(result);
			request.setAttribute("text", result);
			request.getRequestDispatcher("/page/aboutus/aboutUS.jsp").forward(request, response);
		}
		
		
		aud.close();
		
		
		
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occure
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
